package com.common.link.filter;

import cn.hutool.core.util.StrUtil;
import com.common.link.cache.AtlasCache;
import com.common.link.config.LinkConfig;
import com.common.link.model.RemoteUserVO;
import com.common.link.service.RemoteUserService;
import com.common.utils.constant.Constants;
import com.common.utils.tool.StringUtil;
import com.common.link.service.RemoteAtlasService;
import com.common.model.exception.NotAuthorizationException;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 描述：
 * 用户信息获取
 * @author xianggj
 * @Date 2021/9/15 15:37
 **/
@Lazy
@Slf4j
public class UserInfoFilter implements Filter {

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private RemoteAtlasService remoteAtlasService;

    @Autowired
    private LinkConfig linkConfig;

    public static final String USER_KEY = "User-key";

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String requestURI = request.getServletPath();
        if (match(linkConfig.getExcludeUrls(), requestURI)){
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        String token = request.getHeader(LinkConfig.HEADER);
        if (StrUtil.isBlank(token)){
            token = request.getParameter(LinkConfig.WS_HEADER);
        }
        if (StrUtil.isNotBlank(token)){
            try {
                RemoteUserVO currentUser = remoteUserService.getCurrentUser(token);
                request.getSession().setAttribute(USER_KEY, currentUser);
                MDC.put(Constants.USER_ID, currentUser.getId());
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
                log.error("获取用户信息失败"+e.getMessage());
                //过滤器异常拦截不到 只能跳转controller层拦截 这样太麻烦了 这里可以先不管他
                throw new NotAuthorizationException("登录失败");
            }
            if (AtlasCache.isEmpty()){
                remoteAtlasService.getEntityList(token);
                remoteAtlasService.getRelationList(token);
            }
        } else {
            log.error("未获取到登录token");
            throw new NotAuthorizationException("登录失败");
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    private boolean match(String excludeUrls, String requestURI){
        if (StringUtil.isAnyBlank(excludeUrls, requestURI)){
            return false;
        }
        //校验用户必须登录
        AntPathMatcher antPathMatcher = new AntPathMatcher();
        for (String url :excludeUrls.split("\\|")) {
            if (antPathMatcher.match(url, requestURI)){
                return true;
            }
        }
        return false;
    }
}
